﻿$(document).ready(function() {

    $("#btnSend").click(function(evt) {
        var $group = $('#comment-popup');
        var isValid = true;
        $group.find(':input[type!=button]').each(function(i, item) {
            if (!$(item).valid()) {
                if (isValid == true)
                    $(item).focus();
                isValid = false;
            }
        });
        //alert('1');
        if (!isValid)
            evt.preventDefault();
        else {

            $("#btnSend").hide();
            $("#btnClose").hide();
            $("#ajaxloader").show();
            $.ajax({
                async: false,
                dataType: 'json',
                url: "/api/general.ashx",
                type: 'POST',
                data: {
                    action: 'sendCommentToAdmin',
                    Content: $("#Content").val()
                },
                success: function(data) {
                    if (data.result == "-1" || data.result == "1") {
                        window.parent.SendCommentComplete(data.result);
                        return;
                    }
                    $("#btnSend").show();
                    $("#btnClose").show();
                    $("#ajaxloader").hide();

                    if (data.result == "2") {
                        jAlert(CommentToAdminError);
                        return;
                    }
                }
            });
        }
    });

    //Control Behavior
    $('input').focus(function() {
        $(this).addClass("itemfocus");
    });

    $('input').blur(function() {
        $(this).removeClass("itemfocus");
    });

    $('textarea').focus(function() {
        $(this).addClass("itemfocus");
    });
    $('textarea').blur(function() {
        $(this).removeClass("itemfocus");
    });

    //Define Rule For Form
    Valid = $("#formmessage").validate({
        errorPlacement: function(error, element) {
            $(element).attr("title", $(error).html());
        },
        errorClass: "field-error",
        errorElement: "div",
        unhighlight: function(element, errorClass, validClass) {
            $(element).removeClass(errorClass).addClass(validClass);
        },
        rules: {
            Content: {
                required: true
            }
        },
        messages: {
            Content: {
                required: CommentToAdminError
            }
        }
    });
    //end: Validate For Form
});